<?php
/* UNDER CONSTRUCTION 2009-09-24 */

class CommonClient {
	var $version = 0.1;
	var $client = "common";
	var $request = NULL;
	var $location = array();


	/*  This version of logger API can handle following parameters
		"parameter" => array( <to which param we should map this param>,<boolean, is this required or not>, <how to sanitize the param>, <options for filtering>)
	*/
	var $allowed_parameters = array
	(
		"userkey" => array ("userkey",true, FILTER_SANITIZE_STRING),
		"lat" => array ("lat",true, FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION),
		"lon" => array ("lon",true, FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION),
		"speed" => array ("speed",false, FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION),
		"course" => array ("course",false, FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION),
		"heading" => array ("heading",false, FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION),
		"alt" => array ("alt",false, FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION),
		"status" => array ("status",false, FILTER_SANITIZE_STRING),
		"time" => array ("time",false, FILTER_CALLBACK, array("options"=>"fix_time")),
		"date" => array ("date",false, FILTER_CALLBACK, array("options"=>"fix_date")),
		"hdop" => array ("hdop",false, FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION),
	);

	function __construct () {
		Header("Content-type: text/plain");
		print "Plain GET request\n";
		print_r($_GET);
		$this->request = $_GET;
		$this->get_location();
		print "\nsanitized location\n";
		print_r($this->location);
	
		printf("Client: %s, version: %.1f\n\n", $this->client, $this->version);
		print "saving location\n";
		$this->save_location();
	}


	function save_location() {


		while ( list($key, $value) = each ( $this->location ) ) {
			//pffft
		}

		/*
			1) Check if userkey/imei exists
			2) Check if user has current location
				=> if not, then create
			3) Check if user has current track
				=> if not, then create
			4) Update current location
			5) Update current track
			6) Insert location to location archive

			..somewhere in between there we should
			make some calculations like the speed etc
			that we can make out of the data provided.
		*/


	}


	function get_location () {
		// go through all allowed parameters 
		while ( list ($param, $data) = each ($this->allowed_parameters)) {
			// if allowed parameter is set..
			if ( isset( $this->request[$param] ) ) { 
				// filter the parameter with given instructions
				$filtered_param = filter_var($this->request[$param], $data[2], $data[3]);
				if ( !empty ( $filtered_param ) ) {
					$this->location[$data[0]] = $filtered_param; 
				} elseif ( $data[1] === true ) {
					throw new Exception(sprintf("Required parameter '%s' was not provided", $param));
				}
			} elseif ( $data[1] === true ) {
				throw new Exception(sprintf("Required parameter '%s' was not provided", $param));

			}
		}
	}
	
}

?>
